#include <string.h>

#include "debug.h"
#include "random.h"
#include "sms4.h"

void test_sm4()
{
    printf("****** test sm4 ecb *******\n\n");

    int           err = 0;
    int           i;
    sms4_key_t    key;
    unsigned char buf[16];

    unsigned char plaintext[16] = {
        0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
        0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
    };

    unsigned char user_key[16] = {
        0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
        0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
    };

    uint32_t rk[32] = {
        0xf12186f9, 0x41662b61, 0x5a6ab19a, 0x7ba92077, 0x367360f4, 0x776a0c61,
        0xb6bb89b3, 0x24763151, 0xa520307c, 0xb7584dbd, 0xc30753ed, 0x7ee55b57,
        0x6988608c, 0x30d895b7, 0x44ba14af, 0x104495a1, 0xd120b428, 0x73b55fa3,
        0xcc874966, 0x92244439, 0xe89e641f, 0x98ca015a, 0xc7159060, 0x99e1fd2e,
        0xb79bd80c, 0x1d2115b0, 0x0e228aeb, 0xf1780c81, 0x428d3654, 0x62293496,
        0x01cf72e5, 0x9124a012,
    };

    // enc 1 time std output
    unsigned char ciphertext1[16] = {
        0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e,
        0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46,
    };
    // enc 1000000 times std output
    unsigned char ciphertext2[16] = {
        0x59, 0x52, 0x98, 0xc7, 0xc6, 0xfd, 0x27, 0x1f,
        0x04, 0x02, 0xf8, 0x04, 0xc3, 0x3d, 0x3f, 0x66,
    };

    printHex("in", plaintext, 16);
    printHex("key", user_key, 16);

    /* test key scheduling */
    sms4_set_encrypt_key(&key, user_key);

    if (memcmp(key.rk, rk, sizeof(rk)) != 0) {
        printf("\nsms4 key scheduling not passed!\n");
        err++;
        goto end;
    }
    printf("sms4 key scheduling passed!\n");

    /* test encrypt once */
    sms4_encrypt(plaintext, buf, &key);

    if (memcmp(buf, ciphertext1, sizeof(ciphertext1)) != 0) {
        printf("sms4 encrypt not pass!\n");
        err++;
        goto end;
    }
    printHex("enc 1 times out", buf, 16);
    printf("\nsms4 encrypt pass!\n\n");

    /* test encrypt 1000000 times */
    memcpy(buf, plaintext, sizeof(plaintext));
    for (i = 0; i < 1000000; i++) {
        sms4_encrypt(buf, buf, &key);
    }
    printHex("enc 1000000 times out", buf, 16);
    if (memcmp(buf, ciphertext2, sizeof(ciphertext2)) != 0) {
        printf("sms4 encrypt 1000000 times not pass!\n");
        err++;
        goto end;
    }
    printf("sms4 encrypt 1000000 times pass!\n");

    sms4_encrypt(plaintext, buf, &key);
    printHex("enc out", buf, 16);

    unsigned char out[16];
    sms4_set_decrypt_key(&key, user_key);
    sms4_decrypt(buf, out, &key);
    printHex("dec out", out, 16);
    if (memcmp(plaintext, out, 16) == 0) {
        printf("\ndec pass.\n\n");
    } else {
        printf("\ndec failed.\n\n");
        err++;
        goto end;
    }

    printf("\n\n****** test sm4 ecb ex *******\n\n");

    unsigned char ex_in[16];
    unsigned char ex_out[16];
    unsigned char ex_out2[16];
    unsigned char ex_userkey[16];

    vli_get_random(ex_in, 16);
    vli_get_random(ex_userkey, 16);
    printHex("input data", ex_in, 16);
    printHex("key", ex_userkey, 16);

    sms4_key_t ex_key;

    sms4_set_encrypt_key(&ex_key, ex_userkey);
    sms4_encrypt(ex_in, ex_out, &ex_key);

    printHex("enc out", ex_out, 16);

    sms4_set_decrypt_key(&ex_key, ex_userkey);
    sms4_decrypt(ex_out, ex_out2, &ex_key);

    printHex("dec out", ex_out2, 16);
    if (memcmp(ex_in, ex_out2, 16) == 0)
        printf("\n ex test pass.\n");
    else {
        printf("\n ex test failed.\n");
        err++;
    }

    printf("\n\n");

    if (err == 0)
        printf("sms4 all test vectors pass!\n\n");
    else
    end:
        printf("some test vector failed\n\n");

    return;
}

void test_sm4_cbc()
{
    printf("\n\n****** test sm4 cbc ex *******\n\n");
    printf("not implementation yet.\n\n");
    return;
}